package com.sogoodsoft.web.account;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.dao.HibernateUtils;
import com.sogoodsoft.entity.account.Authority;
import com.sogoodsoft.entity.account.Role;
import com.sogoodsoft.service.account.AccountManager;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

/**
 * 角色管理Action.
 * 
 * 演示不分页的简单管理界面.
 * 
 * @author calvin
 */
@Namespace("/account")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "role.action", type = "redirect") })
public class RoleAction extends CrudActionSupport < Role > {

    private static final long serialVersionUID = -4052047494894591406L;

    private static Logger logger = LoggerFactory.getLogger(RoleAction.class);

    private AccountManager accountManager;

    // -- 页面属性 --//
    private Long id;

    private Role entity;

    private List < Role > allRoleList;// 角色列表

    private List < Long > checkedAuthIds;// 页面中钩选的权限id列表

    @SuppressWarnings("unused")
    private List < Authority > allAuthorityList; // 系统所有权限

    private Page < Role > page = new Page < Role >(WebConstants.PAGE_SIZE);

    // -- ModelDriven 与 Preparable函数 --//
    public Role getModel() {
        return entity;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = accountManager.getRole(id);
        } else {
            entity = new Role();
        }
    }

    // -- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        // allRoleList = accountManager.getAllRole();
        List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        // 设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("odr");
            page.setOrder(Page.ASC);
        }
        page = accountManager.searchRole(page, filters);
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        checkedAuthIds = entity.getAuthIds();
        allAuthorityList = this.getAllAuthorityList();
        return INPUT;
    }

    @Override
    public String save() throws Exception {
        // 根据页面上的checkbox 整合Role的Authorities Set.
         HibernateUtils.mergeByCheckedIds(entity.getAuthorityList(), checkedAuthIds, Authority.class);
        // 保存用户并放入成功信息.
        accountManager.saveRole(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        // addActionMessage("保存角色成功");
        return null;
    }

    @Override
    public String delete() throws Exception {
        accountManager.deleteRole(id);
        addActionMessage("删除角色成功");
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

    // -- 页面属性访问函数 --//
    /**
     * list页面显示所有角色列表.
     */
    public List < Role > getAllRoleList() {
        return allRoleList;
    }

    /**
     * input页面显示所有授权列表.
     */
    public List < Authority > getAllAuthorityList() {
        List < Authority > list = accountManager.getAllAuthority();
        if (list == null) {
            logger.debug("权限列表=null");
            list = new ArrayList < Authority >();
        }
        return accountManager.getAllAuthority();
    }

    /**
     * input页面显示角色拥有的授权.
     */
    public List < Long > getCheckedAuthIds() {
        return checkedAuthIds;
    }

    /**
     * input页面提交角色拥有的授权.
     */
    public void setCheckedAuthIds(List < Long > checkedAuthIds) {
        this.checkedAuthIds = checkedAuthIds;
    }

    @Autowired
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }

    public Page < Role > getPage() {
        return page;
    }

    public void setPage(Page < Role > page) {
        this.page = page;
    }

}